package cn.it.controller;

import cn.it.entity.PageResult;
import cn.it.entity.QueryPageBean;
import cn.it.entity.Result;
import cn.it.pojo.Permission;
import cn.it.pojo.Role;
import cn.it.service.RoleService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/role")
@RestController
public class RoleController {

    @Reference
    private RoleService roleService;

    @RequestMapping(value = "/findAll", name = "查询用户角色")
    public Result findAll() {
        try {
            List<Role> role = roleService.findAll();
            return new Result(true, "查询用户权限成功", role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询用户权限失败");
        }
    }

    @RequestMapping(value = "/findRoleIdsByUserId", name = "根据用户ID查询当前用户包含的角色ID，用于页面复选框回显")
    public Result findRoleIdsByUserId(Integer id) {

        try {
            List<Integer> roleIds = roleService.findRoleIdsByUserId(id);
            return new Result(true, "回显角色信息成功", roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "回显角色信息失败");
        }
    }


    @RequestMapping(value = "/findPage", name = "角色分页显示")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.findPage(queryPageBean);
    }

    @RequestMapping(value = "/findPer", name = "查询所有权限")
    public Result findAllPermission() {
        try {
            List<Permission> permissions = roleService.findAllPermission();
            return new Result(true, "查询权限成功", permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询权限失败");
        }

    }

    @RequestMapping(value = "/findById", name = "查询角色信息")
    public Result findById(Integer id) {
        try {
            Role role = roleService.findById(id);
            return new Result(true, "查询角色成功", role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色失败");
        }

    }


    @RequestMapping(value = "/findPermissionIdsByRoleId", name = "根据角色ID查询当前角色包含的权限ID")
    public Result findPermissionIdsByRoleId(Integer id) {
        try {
            List<Integer> roleId = roleService.findPermissionIdsByRoleId(id);
            return new Result(true, "权限回显成功", roleId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "权限回显失败");
        }
    }

    @RequestMapping(value = "/add", name = "根据角色ID查询当前角色包含的权限ID")
    public Result add(Integer[] permissionIds, @RequestBody Role role) {

        try {
            roleService.add(permissionIds, role);
            return new Result(true, "添加角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加角色失败");
        }
    }

    @RequestMapping(value = "/edit", name = "编辑角色")
    public Result edit(@RequestBody Role role, Integer[] permissionIds) {
        try {
            roleService.edit(role, permissionIds);
            return new Result(true, "编辑角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑角色失败");
        }

    }

    //deleteById
    @RequestMapping(value = "/deleteById", name = "删除角色信息")
    public Result deleteById(Integer id) {
        try {
            roleService.deleteById(id);
            return new Result(true,"删除角色信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除角色信息失败");
        }
    }
}
